package com.zero.zeroframesample

import androidx.multidex.MultiDexApplication
import com.drake.brv.utils.BRV
import com.drake.net.NetConfig
import com.drake.net.interceptor.RequestInterceptor
import com.drake.net.okhttp.setRequestInterceptor
import com.drake.net.request.BaseRequest
import com.drake.statelayout.StateConfig
import com.lxj.androidktx.AndroidKTX
import com.tencent.mmkv.MMKV
import com.zero.zeroframe.utils.CrashHandler
import com.zero.zeroframesample.data.Api
import com.zero.zeroframesample.data.AppConfig
import java.util.concurrent.TimeUnit

class App : MultiDexApplication() {

    override fun onCreate() {
        super.onCreate()
        BRV.modelId = BR.m
        MMKV.initialize(this)

        CrashHandler.getInstance().init(this)
        //简单注册，保持默认配置
        AndroidKTX.init(this)
        //Net全局配置
        NetConfig.initialize(Api.BASE_URL, this) {
            // 超时配置, 默认是10秒, 设置太长时间会导致用户等待过久
            connectTimeout(30, TimeUnit.SECONDS)
            readTimeout(30, TimeUnit.SECONDS)
            writeTimeout(30, TimeUnit.SECONDS)

            setRequestInterceptor(object : RequestInterceptor {
                override fun interceptor(request: BaseRequest) {
                    if (AppConfig.token != null) request.setHeader(
                        "Authorization",
                        AppConfig.token!!
                    )
                }
            })
        }

        //缺省页
        StateConfig.apply {
            emptyLayout = com.lxj.androidktx.R.layout._loading_layout_empty
            errorLayout = com.lxj.androidktx.R.layout._loading_layout_error
            loadingLayout = com.lxj.androidktx.R.layout._loading_layout_loading
        }
    }

}